package com.hf.healthy_life.controller;

import com.hf.healthy_life.pojo.User;
import com.hf.healthy_life.service.UserService;
import com.hf.healthy_life.utils.Result;
import com.hf.healthy_life.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    /*登录接口*/
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo) {
        String username = loginVo.getUsername();
        String password = loginVo.getPassword();
        return Result.success(userService.login(username, password));
    }
    /*注册接口*/
    @PostMapping("/register")
    public Result register(String username, String phone, String password) {
   if (userService.register(username,  phone,password)> 0)
        return Result.success("注册成功");
   else
        return Result.error(500, "注册失败");
    }
    /*查询个人资料*/
    @GetMapping("/getInfo/{userId}")
    public Result getUserInfo(@PathVariable("userId") Long userId){
        return Result.success(userService.getUserInfo(userId));
    }
    /*修改个人资料*/
    @PutMapping("/editInfo")
    public Result updateUserInfo(@RequestBody User user) {
        return Result.success(userService.updateUserInfo(user));
    }
    /*修改密码*/
    @PutMapping("/editPassword")
    public Result updatePassword(@RequestBody User user) {
      int result =  userService.editPassword(user);
      if (result == 0)
          return Result.error(500, "修改密码失败");
      else
        return Result.success("修改密码成功");
    }
}
